# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - disable epoll on aarch64
#  - patch to fix padding calculation

pkgbase=percona-server
pkgname=('libperconaserverclient' 'percona-server-clients' 'percona-server')
pkgver=8.4.0_1
_pkgver=${pkgver/_/-}
_myver=${pkgver/_rel*}
pkgrel=1
arch=('x86_64')
makedepends=('cmake' 'zlib' 'lz4' 'zstd' 'libaio' 'systemd-tools' 'pam' 'numactl' 'jemalloc' 'openssl'
             'rpcsvc-proto' 'doxygen' 'graphviz' 'libevent' 'libfido2') # 'boost'
license=('GPL')
url="https://www.percona.com/software/mysql-database/percona-server"
source=("https://www.percona.com/downloads/Percona-Server-${pkgver%.*_*}/Percona-Server-$_pkgver/source/tarball/percona-server-$_pkgver.tar.gz"
        'gcc-14.patch'
        'my.cnf'
        'mysql-user.conf'
        'mysqlrouter-user.conf'
        '0001-fix-LF_PINS-padding-calculation.patch')
sha256sums=('efa3d75ea4cd055b03ed15f6be1a7b472112885a492f4874cc6f6e70d7f2dee4'
            'eeb9c6c8f70dd5eb05f735df0cdfc294365b393e5819f26029320e38714069a6'
            'b467b04d6d06152b2abc33f2a6de63fef0fc922dd5119d2ee1d07d3c1a489731'
            '5d7710fe88ec6d298175a309c0b776142397b119c468830b2865980292ed5da6'
            '4ca7ffdcb2d1716d4f31e4c7dd314e5d76e64f13fdc67c5d81c53650b793f5e0'
            'aa5e960625843b061f47c32b4a267fab1115667b8b7d1ce7ec952efe42eb77af')

prepare() {
	cd $pkgbase-$_pkgver
	rm -v sql/sql_yacc.{cc,h}

	sed -r -e s@/var/run/mysqlrouter@/run/mysqlrouter@ \
	       -e s@lib64/mysql@lib/mysql@ \
	       -i cmake/install_layout.cmake

	for svcfile in mysqld{,@}.service.in; do
		sed '/^PrivateTmp=/ a StateDirectory=mysql mysql-files\nRuntimeDirectory=mysqld' \
		    -i scripts/systemd/$svcfile
	done

	sed '/^PrivateTmp=/ a StateDirectory=mysqlrouter\nRuntimeDirectory=mysqlrouter' \
		-i scripts/systemd/mysqlrouter.service.in

	patch -p1 -i ../gcc-14.patch # Fix build with GCC 14

	patch -p1 -i "$srcdir"/0001-fix-LF_PINS-padding-calculation.patch
}

build() {
	# rm -rf build
	mkdir -p build
	cd build

	[[ $CARCH == "aarch64" ]] && CONFIG="-DHAVE_SYS_EPOLL_H=0"

	cmake "../$pkgbase-$_pkgver" $CONFIG \
		-DCMAKE_C_FLAGS_RELWITHDEBINFO="${CFLAGS} -DNDEBUG" \
		-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="${CXXFLAGS} -DNDEBUG" \
		-Wno-dev \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
		-DBUILD_CONFIG=mysql_release \
		-DREPRODUCIBLE_BUILD=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_DATADIR=/var/lib/mysql \
		-DMYSQL_UNIX_ADDR=/run/mysqld/mysqld.sock \
		-DDEFAULT_CHARSET=utf8mb4 \
		-DDEFAULT_COLLATION=utf8mb4_0900_ai_ci \
		-DWITH_EXTRA_CHARSETS=all \
		-DENABLED_LOCAL_INFILE=ON \
		-DINSTALL_LAYOUT=RPM \
		-DINSTALL_SBINDIR=bin \
		-DINSTALL_LIBDIR=lib \
		-DINSTALL_PLUGINDIR=lib/perconaserver/plugin \
		-DINSTALL_INCLUDEDIR=include/perconaserver \
		-DINSTALL_INFODIR=share/doc/percona-server \
		-DINSTALL_DOCREADMEDIR=share/doc/percona-server \
		-DINSTALL_DOCDIR=share/doc/percona-server \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_STATIC_LIBRARIES=OFF \
		-DWITH_ZLIB=system \
		-DWITH_LZ4=system \
		-DWITH_ZSTD=system \
		-DWITH_SSL=system \
		-DWITH_ICU=system \
		-DWITH_FIDO=system \
		-DWITH_ENCRYPTION_UDF=ON \
		-DWITH_LIBWRAP=OFF \
		-DWITH_MECAB=OFF \
		-DWITH_PAM=ON \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_ROCKSDB=ON \
		-DROCKSDB_DISABLE_AVX2=1 \
		-DROCKSDB_DISABLE_MARCH_NATIVE=1 \
		-DWITH_VALGRIND=OFF \
		-DDEBUG_EXTNAME=OFF \
		-DBUILD_TESTING=OFF \
		-DWITH_UNIT_TESTS=OFF \
		-DWITH_SYSTEMD=1 \
		-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc' \
		-DWITH_NUMA=ON

	make
}

package_libperconaserverclient() {
	pkgdesc='Percona Server client libraries'
	depends=('zlib' 'zstd' 'openssl' 'libfido2')
	optdepends=('libsasl: authentication_ldap_sasl_client plugin')

	cd build
	for dir in include libmysql libservices; do
		make -C $dir DESTDIR="$pkgdir" install
	done

	rm -vrf "$pkgdir"/usr/lib/perconaserver/plugin/debug

	install -Dm755 scripts/mysql_config "$pkgdir"/usr/bin/perconaserver_config
	install -Dm644 "$srcdir/$pkgbase-$_pkgver/man/mysql_config.1" "$pkgdir"/usr/share/man/man1/perconaserver_config.1
	sed s@/lib64@/lib@ -i "$pkgdir"/usr/bin/perconaserver_config

	install -D -m0644 scripts/perconaserverclient.pc "$pkgdir"/usr/lib/pkgconfig/perconaserverclient.pc
	install -D -m0644 "$srcdir/$pkgbase-$_pkgver/support-files/mysql.m4" "$pkgdir"/usr/share/aclocal/perconaserverclient.m4
}

package_percona-server-clients() {
	pkgdesc='Percona Server client tools'
	depends=('libperconaserverclient' 'zlib' 'zstd' 'lz4' 'openssl' 'jemalloc' 'readline')
	conflicts=('mysql-clients')
	provides=("mysql-clients=$_myver" "mariadb-clients=$_myver")

	cd build
	make -C client DESTDIR="$pkgdir" install

	# install man pages
	install -d "$pkgdir"/usr/share/man/man1
	for man in mysql{,admin,check,dump,import,show,slap,binlog}; do
		install -m644 "$srcdir/$pkgbase-$_pkgver/man/$man.1" "$pkgdir"/usr/share/man/man1/$man.1
	done

	# provided by percona-server
	rm "$pkgdir"/usr/bin/mysql{_config_editor,_secure_installation}
}

package_percona-server() {
	pkgdesc='Drop-in replacement for MySQL that provides improved performance, diagnostics, instrumentation and MyRocks storage engine'
	backup=('etc/mysql/my.cnf' 'etc/mysqlrouter/mysqlrouter.conf')
	depends=('libaio' 'systemd-tools' 'pam' 'jemalloc' 'numactl' 'lz4' 'zstd' 'openssl' 'libtirpc' 'curl'
	         'libevent' 'icu')
	optdepends=('perl-dbd-mysql')
	conflicts=('mysql')
	provides=("mysql=$_myver" "mariadb=$_myver")
	options=('emptydirs')

	cd build
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"
	install -Dm644 "$srcdir/my.cnf" etc/mysql/my.cnf
	install -Dm644 "$srcdir/mysql-user.conf" usr/lib/sysusers.d/mysql.conf

	install -dm755 etc/mysqlrouter
	sed -e 's:@ROUTER_RUNTIMEDIR@:/run/mysqlrouter:' \
		-e 's:^logging_folder.*:logging_folder =:' \
		"$srcdir/$pkgbase-$_pkgver/packaging/rpm-common/mysqlrouter.conf.in" \
		> etc/mysqlrouter/mysqlrouter.conf
	install -Dm644 "$srcdir/mysqlrouter-user.conf" usr/lib/sysusers.d/mysqlrouter.conf

	chmod 755 usr
	rm -vrf usr/{cmake,lib/perconaserver/plugin/debug,lib/tmpfiles.d/mysql{,router}.conf}

	# Move documentation
	if [ -f usr/PATENTS ]; then
		mv usr/{PATENTS,README.md} usr/share/doc/$pkgname/
		rm usr/COPYING.*
	fi

	# provided by libperconaserverclient
	rm usr/bin/mysql_config
	rm usr/lib/libperconaserverclient*
	rm -r usr/include/
	rm usr/share/man/man1/mysql_config.1
	rm -r usr/share/aclocal usr/lib/pkgconfig

	# provided by percona-server-clients
	rm usr/bin/mysql{,admin,check,dump,import,show,slap,binlog,test,_migrate_keyring}
	rm usr/lib/perconaserver/plugin/authentication_*_client.so
	rm usr/share/man/man1/mysql{,admin,check,dump,import,show,slap,binlog}.1

	# not needed
	rm -r usr/share/mysql-test
}
